Skip to content

feat(currency): Introduce try_new_code constructors and IsoName fallback#8229

Merged
younies merged 3 commits into
unicode-org:mainfrom
younies:currency-iso-formatting
Jul 21, 2026
Merged

feat(currency): Introduce try_new_code constructors and IsoName fallback#8229
younies merged 3 commits into
unicode-org:mainfrom
younies:currency-iso-formatting

Conversation

@younies

@younies younies commented Jul 14, 2026

Copy link
Copy Markdown
Member

Introduce try_new_code constructor family for formatting with 3-letter ISO currency codes (e.g. USD, EUR) without requiring symbol data payloads, completing the 12-constructor matrix across standard and compact formatting.

Key Changes

  • Complete Constructor Matrix (try_new_code): Implement try_new_code, try_new_compact_code, and try_new_compact_long_code (and their compiled/buffer/unstable variants). These explicitly construct CurrencyFormatterData::Iso, adapting standard symbol fallback layout without requiring CurrencySymbolsV1 data payloads.
  • Remove Optional Payloads (IsoName Fallback): Add CurrencyFormatterData::IsoName to eliminate Option<DataPayload> in full display name formatters (Name). When try_new_name encounters missing localized display names, it cleanly falls back to IsoName, bypassing unnecessary PluralRules instantiation and styling with default unit patterns.
  • Tests: Add unit tests in format.rs (test_code and test_name_fallback_to_iso_name) verifying ISO code formatting and graceful fallback behavior.

Changelog

  • Introduce try_new_code constructors for explicit ISO code formatting across decimal and compact currency formatters.
  • Introduce IsoName fallback variant in try_new_name to remove optional payload overhead when long currency display names are absent.

TAG=agy
CONV=f93d26e4-1f87-41f6-88d1-9069dadd73ee

@younies
younies force-pushed the currency-iso-formatting branch from 2c83b9a to 9416f65 Compare July 14, 2026 14:21
@younies younies changed the title feat(currency): Introduce try_new_code and try_new_iso_code constructors feat(currency): Introduce try_new_code constructor Jul 14, 2026
@younies
younies force-pushed the currency-iso-formatting branch from 9416f65 to 2498c10 Compare July 14, 2026 14:57
@younies
younies marked this pull request as ready for review July 16, 2026 13:28
@younies
younies requested review from a team, Manishearth, robertbastian and sffc as code owners July 16, 2026 13:28

@robertbastian robertbastian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be done after #8235

…llback

Introduce try_new_code, try_new_compact_code, and try_new_compact_long_code constructors for formatting with 3-letter ISO currency codes (e.g. USD, EUR) without requiring the CurrencySymbolsV1 data payload, completing the 12-constructor matrix.

- Adapt standard symbol ISO fallback by directly producing CurrencyFormatterData::Iso in try_new_code constructors.
- Add CurrencyFormatterData::IsoExtended variant and adapt try_new_name to fall back cleanly to IsoExtended when long currency display names are missing, eliminating optional DataPayloads in enum variants.
- Add test_code and test_name_fallback_to_iso_extended unit tests verifying ISO code formatting and long name fallback behaviors.

TAG=agy
CONV=f93d26e4-1f87-41f6-88d1-9069dadd73ee
@younies
younies force-pushed the currency-iso-formatting branch from 2498c10 to 9ab4c5a Compare July 21, 2026 12:25
younies added 2 commits July 21, 2026 12:30
Rename CurrencyFormatterData::IsoExtended to IsoName to make it clear that it represents the fallback representation for full display name formatting.

TAG=agy
CONV=f93d26e4-1f87-41f6-88d1-9069dadd73ee
When full display name formatting falls back to an invariant ISO currency code (IsoName), evaluating cardinal plural rules is unnecessary since ISO codes do not decline by grammatical count.

- Remove plural_rules field from CurrencyFormatterData::IsoName.
- Avoid loading PluralRules when long display names are absent during try_new_name initialization.
- Use default unit pattern (other) directly when styling IsoName expressions.

TAG=agy
CONV=f93d26e4-1f87-41f6-88d1-9069dadd73ee
@younies younies changed the title feat(currency): Introduce try_new_code constructor feat(currency): Introduce try_new_code constructors and IsoName fallback Jul 21, 2026
@younies

younies commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

@robertbastian : you can review this now

@younies
younies requested a review from robertbastian July 21, 2026 12:53
#[cfg(feature = "compiled_data")]
pub fn try_new_compact_code(
prefs: CurrencyFormatterPreferences,
currency_code: &CurrencyCode,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, pre-existing: we should take CurrencyCode by value, it's a small Copy struct

let value = "12345.67".parse().unwrap();

let fmt_code_en = CurrencyFormatter::try_new_code(prefs_en, &currency_usd).unwrap();
assert_writeable_eq!(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test compact as well?


#[derive(Debug)]
pub(crate) enum CurrencyFormatterData {
Iso {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call this IsoSymbol then

@younies
younies merged commit 36d5309 into unicode-org:main Jul 21, 2026
35 checks passed
@younies
younies deleted the currency-iso-formatting branch July 21, 2026 14:48
@younies

younies commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

applying all the comments in a following PR to rebase the other PRs

robertbastian pushed a commit that referenced this pull request Jul 21, 2026
… to IsoSymbol (#8246)

Address review feedback left on #8229 from @robertbastian:
- Change all `try_new_*` constructor signatures in `CurrencyFormatter`
to take `CurrencyCode` by value instead of by reference (`CurrencyCode`
is a small `Copy` struct).
- Rename `CurrencyFormatterData::Iso` enum variant to `IsoSymbol` for
clarity and symmetry with `IsoName`.
- Add unit test coverage (`test_compact_code`) for both short and long
compact ISO currency code formatting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants